using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace Elephant.Framework.Services
{
    public class ContentProvider : GameComponent, IContentService
    {
        ContentManager content;

        public ContentProvider(Game game)
            : base(game)
        {
            if (game.Services.GetService(typeof(IContentService)) != null) {
                throw new ArgumentException("ContentManager already present.");
            }

            game.Services.AddService(typeof(IContentService), this);

            content = new ContentManager(game.Services);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                if (this.Game.Services.GetService(typeof(IContentService)) == this) {
                    this.Game.Services.RemoveService(typeof(IContentService));
                }
            }

            base.Dispose(disposing);
        }

        #region IContentManagerService Members

        public T Load<T>(string assetName)
        {
            return content.Load<T>(assetName);
        }

        public void Unload()
        {
            content.Unload();
        }

        public ContentManager ContentManager
        {
            get 
            { 
                return content; 
            }
        }

        #endregion
    }
}
